package com.crcc.common.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 阿里云工具类
 * @author cc19_dongy
 */
public class AliyunUtil {
    private static String accessKeyId = "LTAI4G1o7ZeFukZ8JCVq1HNr";
    private static String accessSecret = "ZR0yuF1cYcsLzNKe9ACvKTRJq5Yiy8";
    private static String regionId = "cn-hangzhou";//区域
    private static String signName = "深蓝技术";//签名
    private static String codeTemplateCode = "SMS_198665025";//验证码短信模板

    /**
     * 发送验证码短信
     */
    public static String sendCodeSMS(String phone, String code) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", codeTemplateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
            return "fail";
        } catch (ClientException e) {
            e.printStackTrace();
            return "fail";
        }
    }

    public static void main(String[] args) {
        System.out.println(AliyunUtil.sendCodeSMS("18240369675", "123456"));
    }
}
